"use strict";

import React from 'react';
import RootSiblings from 'react-native-root-siblings'
import CascadeDatePickerContainer from './CascadeDatePickerContainer';

let sibling = undefined
class CascadeDatePicker extends React.Component {

    static displayName = 'CascadeDatePicker';
    static propTypes = CascadeDatePickerContainer.propTypes;

    static show = (options) => {

        sibling = new RootSiblings(
            <CascadeDatePickerContainer
                {...options}
                hide={false}
                destroy={()=>{CascadeDatePicker.hide()}}
            />
        );
        return sibling;
    };

    static hide = () => {
        if (sibling instanceof RootSiblings) {
            sibling.destroy();
        } else {
            console.warn(`CascadeDatePicker.hide expected a \`RootSiblings\` instance as argument.\nBut got \`${typeof sibling}\` instead.`);
        }
    };

    _CascadeDatePicker = null;

    constructor(props) {
        super(props);
    };

    show = ()=>{
        this.container.show()
    }

    hide = ()=>{
        this.container._close()
    }

    componentDidMount = () => {
        this._CascadeDatePicker= new RootSiblings(
            <CascadeDatePickerContainer
                ref={(obj) =>{this.container = obj}}
                {...this.props}
            />
        );
    };


    componentDidUpdate(){
        this._CascadeDatePicker.update(
            <CascadeDatePickerContainer
                ref={(obj) =>{this.container = obj}}
                {...this.props}
            />
        );
    }

    componentWillUnmount = () => {
        this._CascadeDatePicker.destroy();
    };

    render(){
        return null;
    }

}

module.exports = CascadeDatePicker;
